
package com.nd.rj.common.login.dbreposit;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.nd.rj.common.R;
import com.nd.rj.common.util.db.BaseDBHelper;
import com.nd.rj.common.util.db.IDataBaseRef;
import com.nd.rj.common.util.db.SqliteHelper;

/**
 * @ClassName: CfgDBHelper
 * @Description: 配置数据库操作类
 * @author Administrator
 * @date 2012-2-1 下午02:07:22
 */
public class CfgDBHelper extends BaseDBHelper {

    // 数据库版本号
    private static final int DB_VERSION = 1;

    // 数据库名称
    private static final String DB_NAME = "Config.cfg";

    private static IDataBaseRef mHelper;

    private CfgDBHelper() {
        TAG = "CfgDBHelper";
    }

    public synchronized static IDataBaseRef getInstance() {
        if (mHelper == null)
            mHelper = new CfgDBHelper();
        return mHelper;
    }
    
    @Override
    public boolean DbValid() {
        if ((mSQLiteDatabase == null) || (!mSQLiteDatabase.isOpen())){
            return false;
        }
        
        return true;
    }

    @Override
    protected synchronized int openDB(Context context, String username) {
        int ireturn = R.string.nd_open_db_error;
        if ((mSQLiteDatabase == null) || (!mSQLiteDatabase.isOpen())) {
            // 先关闭
            close();
            mSqliteHelper = new SqliteHelper(context, DB_NAME, null, DB_VERSION, this);
            if (mSqliteHelper != null) {
                mSQLiteDatabase = mSqliteHelper.getWritableDatabase();
                mDBName = DB_NAME;
                ireturn = 0;
            } else {
                Log.v(TAG, mDBName + DB_NAME + "open db error");
            }
            mDBName = DB_NAME;
        } else {
            Log.v(TAG, mDBName + " is already open !");
        }
        return ireturn;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    /**
     * @Title: createTable
     * @Description: (建表)
     * @author Administrator
     * @date 2012-2-1 下午03:35:02
     * @return
     * @return int
     * @throws
     */
    private int createTable(SQLiteDatabase db) {
        StringBuilder sb = new StringBuilder(400);
        sb.append("Create  TABLE USERINFO(");
        sb.append("[UAP_UID] BIGINT NOT NULL,");
        sb.append("[USER_NAME] varchar(300),");
        sb.append("[USER_NICK_NAME] varchar(300),");
        sb.append("[USER_PASS] varchar(300),");
        sb.append("[USER_PASS_MD5] varchar(300),");
        sb.append("[USER_PASS_MIXED_MD5] varchar(300),");
        sb.append("[BLOWFISH] varchar(60),");
        sb.append("[TICKET] varchar(255),");
        sb.append("[ISADMIN] INT,");
        sb.append("[OAP_UNIT_ID] BIGINT,");
        sb.append("[OAP_UID] BIGINT,");
        sb.append("[ISSAVEACCOUNT] bit,");
        sb.append("[ISAUTOLOGIN] bit, ");
        sb.append("[LAST_LOGIN_DT] datetime, ");
        sb.append("[VAR_EXT] text, ");
        sb.append("[ISPHONE] INT,");
        sb.append("[sex] INT,");
        sb.append("[prestige] INT,");
        sb.append("[wealth] INT,");
        sb.append("[grade] INT,");
        sb.append("[flower] INT,");
        sb.append("[realname] text, ");
        sb.append("[uniquename] text, ");
        sb.append("Primary Key(UAP_UID) ) ");
        db.execSQL(sb.toString());
        return 0;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        if (!tableExists(db, "USERINFO")) {
            // 新的数据库要建表
            createTable(db);
        }
    }
}
